package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.home.listener.OnItemClickListener;
import com.rongshu.view.userInfo.entity.PaySetMeal;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PaySetMealAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext = null;
    private List<PaySetMeal.ResponseBean> paySetMeals = null;
    //interface
    private OnItemClickListener listener = null;

    public PaySetMealAdapter(Context context) {
        this.mContext = context;
    }

    public void setPaySetMeals(List<PaySetMeal.ResponseBean> list) {
        this.paySetMeals = list;
    }

    public List<PaySetMeal.ResponseBean> getPaySetMeals() {
        return this.paySetMeals;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_pay_set_meal_item_view, parent, false);
        return new PaySetMealViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        PaySetMealViewHolder holder = (PaySetMealViewHolder) viewHolder;
        PaySetMeal.ResponseBean paySetMeal = paySetMeals.get(position);
        holder.txt_pay_money.setTextColor(paySetMeal.isIs_select() ?
                ContextCompat.getColor(mContext, R.color.color_3cc051) :
                ContextCompat.getColor(mContext, R.color.color_4a4a4a));
        holder.txt_pay_money.setText((int) paySetMeal.getRmb() + "元");
        holder.txt_pay_money_get_hongshuye.setTextColor(paySetMeal.isIs_select() ?
                ContextCompat.getColor(mContext, R.color.color_3cc051) :
                ContextCompat.getColor(mContext, R.color.color_9f9f9f));
        holder.txt_pay_money_get_hongshuye.setText("购买" + paySetMeal.getRongshu_leaf_num() + "榕树叶");
        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return paySetMeals == null ? 0 : paySetMeals.size();
    }


    public class PaySetMealViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.pay_money_layout)
        RelativeLayout layout;
        @BindView(R.id.txt_pay_money)
        TextView txt_pay_money;
        @BindView(R.id.txt_pay_money_get_hongshuye)
        TextView txt_pay_money_get_hongshuye;

        public PaySetMealViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
